function [output] = getdistributions(whichgroup, vars)
%GETDISTRIBUTIONS represent distributions of specified variables for a group 
%of patients

% whichgroup = {'lvef'} or {'surv'} or {'vsp'}

% vars = {'hr','pulsep','urine','lactate'}

if nargin < 2
    vars = {'hr','pulsep'};
end

if nargin < 1
    whichgroup = {'surv'};
end

% % get labels
% for v = 1 : length(vars)
%     switch vars{v}
%         case 'hr'
%             LABEL{v} = 'Heart Rate';
%         case 'pulsep'
%             LABEL{v} = 'Pulse Pressure';
%         case 'urine'
%             LABEL{v} = 'Urine Output';
%         case 'lactate'
%             LABEL{v} = 'Lactate';
%     end
% end


%% Check for patients with missing data and flag data

output.missingPts = []; % [index] Patients with no data in both cohorts
output.flagPts = []; % [index] Patients with data in both cohorts

if strcmp(whichgroup{1},'surv') 
    % Check whether there are patients who belong to surv and non-surv
    for i = 1 : length(vars)
        X = load(sprintf('../data/%s_cohorts_%s.mat',whichgroup{1},vars{i}));
        for j = 1 : size(X.Xd,1)
            if isempty(X.Xd{j,1}) && isempty(X.Xd{j,2})
                output.missingPts = [output.missingPts; j];
            elseif ~isempty(X.Xd{j,1}) && ~isempty(X.Xd{j,2})
                output.flagPts = [output.flagPts; j];
            end
        end
    end
end
clear X;

%% Represent distributions of data

% Determine histograms based on the mean of values (be careful with this)
ISMEAN = 0;
for v = 1 : length(vars)
    X = load(sprintf('../data/%s_cohorts_%s.mat',whichgroup{1},vars{v}));
    data{1} = []; data{2} = [];
    if ISMEAN
        data{1} = X.Xall{1}(:,2);
        data{2} = X.Xall{2}(:,2);
    else
        for i = 1 : size(X.Xd,1)
            if ~isempty(X.Xd{i,1})
                data{1} = [data{1}; X.Xd{i,1}(:,2)];
            end
            if ~isempty(X.Xd{i,2})
                data{2} = [data{2}; X.Xd{i,2}(:,2)];
            end
        end
    end
    
    switch vars{v}
        case 'hr'
            x = 10:1:300; figure;
            subplot(211); [nelements1,xcentres1] = hist(data{1},x); % Plot cohort 1
            bar(xcentres1,nelements1); xlim([30 250]);
            xlabel('Heart Rate'); ylabel('Counts'); 
            legend(sprintf('%s_1',whichgroup{1}));
            subplot(212); [nelements2,xcentres2] = hist(data{2},x); % Plot cohort 2
            bar(xcentres2,nelements2); xlim([30 250]);
            xlabel('Heart Rate'); ylabel('Counts');
            legend(sprintf('%s_2',whichgroup{1}));
            output.hr.x1 = xcentres1; output.hr.y1 = nelements1;
            output.hr.x2 = xcentres2; output.hr.y2 = nelements2;
            output.hr.data1 = data{1}; output.hr.data2 = data{2};
        case 'pulsep'
            x = 0:1:250; figure; % good range???
            subplot(211); [nelements1,xcentres1] = hist(data{1},x); % Plot cohort 1
            bar(xcentres1,nelements1); xlim([0 200]);
            xlabel('Pulse Pressure'); ylabel('Counts'); 
            legend(sprintf('%s_1',whichgroup{1}));
            subplot(212); [nelements2,xcentres2] = hist(data{2},x); % Plot cohort 2
            bar(xcentres2,nelements2); xlim([0 200]);
            xlabel('Pulse Pressure'); ylabel('Counts');
            legend(sprintf('%s_2',whichgroup{1}));
            output.pulsep.x1 = xcentres1; output.hr.y1 = nelements1;
            output.pulsep.x2 = xcentres2; output.hr.y2 = nelements2;
            output.pulsep.data1 = data{1}; output.pulsep.data2 = data{2};
        case 'urine'
            x = 0:1:250; figure; % good range???
            subplot(211); hist(data{1},x); % Plot cohort 1
            xlabel('Urine Output'); ylabel('Counts'); 
            legend(sprintf('%s_1',whichgroup{1}));
            subplot(212); hist(data{2},x); % Plot cohort 2
            xlabel('Urine Output'); ylabel('Counts');
            legend(sprintf('%s_2',whichgroup{1}));
        case 'lactate'
            LABEL{v} = 'Lactate';
    end
    clear X;
end



end

